#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class Solution {
public:
    bool check[9];
    vector<vector<int>> ans;
    vector<int> path;
    void dfs(vector<int>& nums)
    {
        if (nums.size() == path.size())
        {
            ans.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); ++i)
        {
            if (check[i] == true || (i != 0 && nums[i] == nums[i - 1] && check[i - 1] == false)) continue;
            check[i] = true;
            path.push_back(nums[i]);
            dfs(nums);
            check[i] = false;
            path.pop_back();
        }
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        dfs(nums);
        return ans;
    }
};